package com.dotflat.xj.repository;

import com.dotflat.xj.domain.Target;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TargetRepository extends JpaRepository<Target, String>, JpaSpecificationExecutor<Target> {

    @Query(value="SELECT * FROM xj_target  where status= 0 and areaId in ('0',?1)  ",nativeQuery = true)
    List<Target> getAllAreaId(String areaId);

    @Query(value="SELECT * FROM xj_target  where  status= :status  and  if(:typeName != '',  typeName like concat('%',  :typeName, '%'), 1=1 )" +
            " AND areaId in ('0', :areaId)  AND groupId in ('0', :groupId)  LIMIT :beginRow,:size",nativeQuery = true)
    List<Target> getList(@Param("areaId")String areaId,@Param("typeName") String typeName,@Param("status") String status, @Param("groupId")String groupId,@Param("beginRow")Integer beginRow, @Param("size")Integer size);


    @Query(value="SELECT count(id) FROM xj_target  where  status= :status  and  if(:typeName != '',  typeName like concat('%',  :typeName, '%'), 1=1 )" +
            " AND areaId in ('0', :areaId)   AND groupId in ('0', :groupId) ",nativeQuery = true)
    Integer getListCount(@Param("areaId")String areaId,@Param("typeName") String typeName,@Param("status") String status, @Param("groupId")String groupId);


    Target findAllById(String id);

    @Query(value="SELECT * FROM xj_target  where status= 0 and targetType =:targetType and areaId in ('0',:areaId)   AND groupId in ('0', :groupId) ",nativeQuery = true)
    List<Target> targetTypeList(@Param("areaId")String areaId,@Param("targetType") String targetType, @Param("groupId")String groupId);


    @Query(value="SELECT * FROM xj_target  where status= 0 and areaId in ('0',?1)  AND groupId in ('0', ?2) GROUP BY sort ",nativeQuery = true)
    List<Target> getAreaSort(String areaId, String groupId);

    @Query(value="SELECT * FROM xj_target  where status= 0 and areaId in ('0',?1)  AND groupId in ('0', ?2)  GROUP BY typeName ",nativeQuery = true)
    List<Target> getAreaTypeName(String areaId, String groupId);

    Target findByTypeNameAndTargetTypeAndSortAndRecordTypeAndCycleAndConclusionType(String typeName,String targetType,String sort,String recordType,String cycle,String conclusionType);
}
